﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using OgilvyOne.Model;
using OgilvyOne.Utility;
using OgilvyOne.DALFactory;

namespace OgilvyOne.BLL
{
    /// <summary>
    /// Cms解析基类
    /// </summary>
    abstract class CommonModel : ConConstants
    {
        protected CmsVo vo;
        public CmsVo CmsVo
        {
            get { return vo; }
        }
        //内容
        protected CmsArticleModel article;
        //栏目相关
        protected MenuModel menupage;
        public MenuModel MenuModel
        {
            get { return menupage; }
        }
        //公共方法相关
        protected ContentHtmlPage htmlpage;
        protected string html = ""; //模板HTML
        public CommonModel(CmsVo vo)
        {
            this.vo = vo;
            if (vo != null)
            {
                this.article = vo.CmsArticleInfo;

                this.html = vo.TempInfo.TemCotent;
                if (vo.Cid > 0)
                {//存在栏目ID
                    menupage = new MenuModel(vo.Cid, vo.Aid);
                }
                else
                {
                    menupage = new MenuModel();
                }
            }
            else
            {
                menupage = new MenuModel();
            }
            htmlpage = new ContentHtmlPage(menupage);
        }
        /// <summary>
        /// Cms 模板标签正则定义 形如: <cms:content value="GetTest"></cms:content>
        /// </summary>
        static Regex regex = new Regex("<cms:\\w+\\s+value=[\'|\"][\\w\\.\\s\\*]+[\'|\"][\\s\\w\'\"\\=]*>[\\[\\]\\&\\;\\!\'\"\\=<>/\\w\\s]*</cms:\\w+>");

        public string GetHtml()
        {


            MatchCollection mc = regex.Matches(html);

            foreach (Match m in mc)
            {
                string mv = m.Value;
                html = StringUtil.ReplaceStr(html, mv, LabelProxy.GetCmsLabel(this, mv).GetLabelHtml());
            }

            return html;
        }

        /// <summary>
        /// 当前内容信息
        /// </summary>
        /// <returns></returns>
        public CmsArticleModel GetConInfo()
        {
            return vo.CmsArticleInfo;
        }
        /// <summary>
        /// 当前栏目信息
        /// </summary>
        /// <returns></returns>
        public ColumnModel GetColumnInfo()
        {
            return menupage.GetColumn();
        }
     
        /// <summary>
        /// 父栏目信息
        /// </summary>
        /// <returns></returns>
        public ColumnModel GetParentColumnInfo()
        {
            return menupage.GetColumn(menupage.GetColumn().Parentid);
        }

        /// <summary>
        /// 当前内容标题
        /// </summary>
        /// <returns></returns>
        public string GetTitle()
        {
            if (vo.CmsArticleInfo != null)
            {
                return vo.CmsArticleInfo.Title;
            }
            return "";
        }
        /// <summary>
        /// 副标题1
        /// </summary>
        /// <returns></returns>
        public string GetSubTitle()
        {
            if (vo.CmsArticleInfo != null && vo.CmsArticleInfo.Mores != null)
            {
                if (vo.CmsArticleInfo.Mores.Subtitle != null && vo.CmsArticleInfo.Mores.Subtitle != "")
                {
                    return  vo.CmsArticleInfo.Mores.Subtitle ;
                }
            }
            return "";
        }
        /// <summary>
        /// 副标题2
        /// </summary>
        /// <returns></returns>
        public string GetSubTitle2()
        {
            if (vo.CmsArticleInfo != null && vo.CmsArticleInfo.Mores != null)
            {
                if (vo.CmsArticleInfo.Mores.Subtitle2 != null && vo.CmsArticleInfo.Mores.Subtitle2 != "")
                {
                    return vo.CmsArticleInfo.Mores.Subtitle2 ;
                }
            }
            return "";
        }
       

        public string GetMenuListTitle()
        {
            return menupage.GetMenuListTitle();
          
        }
        /// <summary>
        /// 当前内容详细内容
        /// </summary>
        /// <returns></returns>
        public string GetContent()
        {
            if (vo.CmsArticleInfo != null)
            {
                return vo.CmsArticleInfo.Content;
            }
            return "";
        }
        /// <summary>
        /// 内容来源
        /// </summary>
        /// <returns></returns>
        public string GetContentSource()
        {
            if (vo.CmsArticleInfo != null)
            {
                if (vo.CmsArticleInfo.Source != null && vo.CmsArticleInfo.Source != "")
                {
                    string s =  vo.CmsArticleInfo.Source ;
                    if (vo.CmsArticleInfo.Sourceurl != null && vo.CmsArticleInfo.Sourceurl != "")
                    {
                        s = "<a href=\"" + vo.CmsArticleInfo.Sourceurl + "\" target=\"_blank\">" + s + "</a>";
                    }
                    return "<p>" + "（来源：" + s + "）</p>";
                }
            }
            return "";
        }
        public virtual string GetTitleImg()
        {
            if (vo.Aid > 0)
            {
                IList imgs = CmsArticleFrontFactory.create().GetImgs(vo.Aid, CmsConstants.IMG_TYPE_TITLE);
                if (imgs.Count > 0)
                {
                    CmsArticleImgModel img = (CmsArticleImgModel)imgs[0];
                    return ConConstants.UPLOADFILES_IMAGE_PATH + img.ImgFileName;
                }
            }
            /*if (vo.CmsArticleInfo != null)
            {
                return "/uploadfiles/image/" + vo.CmsArticleInfo.Titleimg;
            }*/
            return "";
        }
        //普通图
        public string GetContentImg()
        {
            if (vo.Aid > 0)
            {
                IList imgs = CmsArticleFrontFactory.create().GetImgs(vo.Aid, CmsConstants.IMG_TYPE_COMMON);
                if (imgs.Count > 0)
                {
                    CmsArticleImgModel img = (CmsArticleImgModel)imgs[0];
                    return ConConstants.UPLOADFILES_IMAGE_PATH + img.ImgFileName;
                }
            }
            return "";
        }

        public string GetTitleImgTag()
        {
            string img = GetTitleImg();
            if (img != "")
            {
                return "<img src=\"" + img + "\" />";
            }
            return "";
        }
        //title/meta/seo
        public string GetMeta()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<title>");
            ColumnModel cm = menupage.GetColumn(); //当前栏目
            if (vo.CmsArticleInfo != null)
            {//内容
                if (vo.CmsArticleInfo.Articletitle != null && vo.CmsArticleInfo.Articletitle.Trim() != "")
                {
                    sb.Append(vo.CmsArticleInfo.Articletitle);
                }
                else
                {
                    sb.Append(GetTitle()).Append(" - ");
                    if (cm.Classid != ConConstants.HOME_ID)
                    {
                        sb.Append(cm.Classname).Append(menupage.GetMenuListTitle()).Append(" - ");
                    }
                    sb.Append(ConConstants.WEB_NAME);
                }
            }
            else
            {//栏目

                if (cm.Columntitle != null && cm.Columntitle.Trim() != "")
                {
                    sb.Append(cm.Columntitle);
                }
                else
                {
                    if (cm.Classid == ConConstants.HOME_ID)
                    {
                        sb.Append(ConConstants.WEB_NAME);
                    }
                    else
                    {
                        sb.Append(cm.Classname).Append(menupage.GetMenuListTitle()).Append(" - ").Append(ConConstants.WEB_NAME);
                    }
                }
            }

            sb.Append("</title>\n");
            if (vo.CmsArticleInfo != null)
            {//内容SEO
                if (vo.CmsArticleInfo.Articlekeyword != null && vo.CmsArticleInfo.Articlekeyword.Trim() != "")
                {//有设定
                    sb.Append("<meta name=\"Keywords\" content=\"");
                    sb.Append(vo.CmsArticleInfo.Articlekeyword);
                    sb.Append("\" />\n");

                    sb.Append("<meta name=\"Description\" content=\"");
                    sb.Append(vo.CmsArticleInfo.Articledesc);
                    sb.Append("\" />\n");
                }
                else
                {//无设定
                    sb.Append("<meta name=\"Keywords\" content=\"");
                    sb.Append(vo.CmsArticleInfo.Title);
                    sb.Append("\" />\n");

                    sb.Append("<meta name=\"Description\" content=\"");
                    sb.Append(ContentHtmlPage.GetSubString(vo.CmsArticleInfo.Content, 100));
                    sb.Append("\" />\n");
                }
                sb.Append(menupage.CodeMeta());
            }
            else
            {//栏目SEO
                sb.Append(menupage.GetColumntSEO());
            }
            return sb.ToString();
        }
        //当前位置
        public string GetPositionNavi()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(menupage.GetPositionNavi());
            if (vo.Aid > 0 && vo.CmsArticleInfo != null)
            {
                sb.Append("\n");
                sb.Append("<script type=\"text/javascript\">\n");
                sb.Append("_ajax('/cn/addarticlenum.aspx?aid=" + vo.Aid + "&now='+new Date().getTime(),'',null);\n");
                sb.Append("</script>");
                sb.Append("\n");
            }
            return sb.ToString();
        }
        //栏目 Banner (kv)
        public string GetColumnBanner()
        {
            if (vo.Aid > 0)
            {//文章banner
                IList imgs = CmsArticleFrontFactory.create().GetImgs(vo.Aid, CmsConstants.IMG_TYPE_KV);
                if (imgs.Count > 0)
                {
                    return ConConstants.UPLOADFILES_IMAGE_PATH + ((CmsArticleImgModel)imgs[0]).ImgFileName;
                }
            }
            return menupage.GetColumntTitleImg();
        }
        
        public string GetArticleContentImg()
        {
            IList ilist = CmsArticleFrontFactory.create().GetImgs(vo.Aid, CmsConstants.IMG_TYPE_COMMON);
            if (ilist.Count > 0)
            {
                return "<img src=\"" + ConConstants.UPLOADFILES_IMAGE_PATH + ((CmsArticleImgModel)ilist[0]).ImgFileName + "\" />";
            }
            return "";
        }

        //////------- 取栏目第一篇文章 strat
        private CmsArticleModel firstArticle = null;

        public CmsArticleModel GetFirstArticle()
        {
            if (firstArticle == null)
            {
                int _cid = vo.Cid;
                ColumnModel cm = menupage.GetColumn();
                if (cm.Templatedetail == 0)
                {//无内容
                    _cid = ((ColumnModel)menupage.GetSonColumn(_cid)[0]).Classid;
                }
                IList cs = CmsArticleFrontFactory.create().GetArticleOrderList(_cid, 1);
                if (cs.Count > 0)
                {
                    firstArticle = (CmsArticleModel)cs[0];
                    if (vo.Preview == 1)
                    {//预览
                        firstArticle = new CmsArticleBll().GetCmsArticleModel(firstArticle.Articleid);
                    }
                    else
                    {
                        firstArticle = CmsArticleFrontFactory.create().getCmsArticle(firstArticle.Articleid);
                    }
                }
            }
            return firstArticle;
        }
        /// <summary>
        /// 栏目第一篇文章URL
        /// </summary>
        /// <returns></returns>
        public string GetFirstArticleUrl()
        {
            if (GetFirstArticle() != null)
            {
                return menupage.GetArticleUrl(GetFirstArticle());
            }
            return "";
        }

        public string GetFirstTitle()
        {
            if (GetFirstArticle() != null)
            {
                return GetFirstArticle().Title;
            }
            return "";
        }
        public string GetFirstContent()
        {
            if (GetFirstArticle() != null)
            {
                return GetFirstArticle().Content;
            }
            return "";
        }
        public string GetFirstTitleImg()
        {
            if (GetFirstArticle() != null)
            {
                string img = GetFirstArticle().Titleimg;
                if (img != null && img != "")
                {
                    return "<img src=\"" + ConConstants.UPLOADFILES_IMAGE_PATH + img + "\" />";
                }
            }
            return "";
        }
        

        public string GetFirstArticleContentImg()
        {
            IList ilist = CmsArticleFrontFactory.create().GetImgs(GetFirstArticle().Articleid, CmsConstants.IMG_TYPE_COMMON);
            if (ilist.Count > 0)
            {
                return "<img src=\"" + ConConstants.UPLOADFILES_IMAGE_PATH + ((CmsArticleImgModel)ilist[0]).ImgFileName + "\" />";
            }
            return "";
        }
        //////-----------------------end

        public string GetColumnTitleImg()
        {
            ColumnModel cm = menupage.GetColumn();
            if (cm.Ttile_img != null && cm.Ttile_img != "")
            {
                return "<img src=\""+ConConstants.UPLOADFILES_IMAGE_PATH+ cm.Ttile_img + "\" />";
            }
            return "";
        }

        
    }
}
